<?php
	class Admin_SlideshowController extends My_Controller_Action{
		private $_model = '';
		private $_moduleName = '';
		private $_controllerName = '';
		private $_actionName = '';
		private $_actionMain = '';
		private $_params = array ();
		private $_userId = '';
		
		public function init(){  
			$template_path = TEMPLATE_PATH ."/admin";
			$this->loadTemplate($template_path,'template.ini','template');
			$this->_model = new Admin_Model_Slideshow();
			$this->_moduleName = $this->_request->getModuleName();
			$this->_controllerName = $this->_request->getControllerName();
			$this->_actionName = $this->_request->getActionName();
			$this->_params = $this->_request->getParams();
			$this->_actionMain = '/' . $this->_moduleName . '/' . $this->_controllerName . '/';
	
			$this->view->params = $this->_params;
			$this->view->controllerName = $this->_controllerName;
		
			$auth = Zend_Auth::getInstance();
			$infoUser = $auth->getIdentity();
			$this->_userId = $infoUser['userId'];
			
			
			My_Auth_User::authenticate();
		}
		public function indexAction(){
			
			$this->_redirect("/{$this->_moduleName}/{$this->_controllerName}/show-all");
			
		}
		public function showAllAction(){
			$result = $this->_model->getAll();
			$this->view->list_slideshow = $result;
		}
		public function addAction(){
			$form = new Admin_Form_Slideshow();
			
			if($this->_request->isPost() ){
				$data = $this->_request->getPost();
				$valid = new Admin_Form_Validators_SlideshowValid($data);
				
				if( $form->isValid($data) && $valid -> isValid() == TRUE ){
					$info = $form->getValues();
					$db = Zend_Db_Table::getDefaultAdapter();
					$db->beginTransaction();
					try{
						
						$fileupload = new My_File_Upload();
						$info['image'] = $fileupload->upload('image','slideshow', array('task' => 'rename', 'isThumb' => TRUE));

						$id = $this->_model->addItem($info);
						
					}
					catch (Exception $e){
						$db->rollBack();
						throw $e;
					}
					if ($id) {
						if (isset ( $this->_params ['btnSave'] )) {
							$this->view->popupMessage = $this->_helper->popupMessage ( MESS_ADD, $this->_actionMain );
						} elseif (isset ( $this->_params ['btnSaveAdd'] )) {
							$this->_redirect ( "/{$this->_moduleName}/{$this->_controllerName}/{$this->_actionName}" );
						}
					}
				}
				else{ 
					$this->view->error = $valid->messages;
				}
			}
			
			$this->view->form = $form;
		}
		public function editAction(){
			$id = $this->_params['id'];
			$form = new Admin_Form_Slideshow();
			if($id){
				$data = $this->_model->getInfo($id);
				
			}
			if($this->_request->isPost()){
				
				$data_new = $this->_request->getPost();
				$valid = new Admin_Form_Validators_SlideshowValid($data);
				$db = Zend_Db_Table::getDefaultAdapter();
				$db->beginTransaction();
				$form->isValid($data); 
				
				if($form->isValid($data_new) && $valid->isValid() == TRUE){
					$info = $form->getValues();
				try{
						
						$fileupload = new My_File_Upload();
						$info['image'] = $fileupload->upload('image','slideshow', array('task' => 'rename', 'isThumb' => TRUE));
						if($info['image'] != ""){
							$fileupload->removeFile(UPLOAD_PATH."/slideshow/".$data['image']);
						}
						
						$slideshow = $this->_model->editItem($id,$info);
						
					}
						catch (Exception $e){
							$db->rollBack();
							throw $e;
						}
					if($slideshow){
						echo "ok";exit;
					}
					else{
						echo "fail";exit;
					}
				}else{
					$this->view->error = $valid->messages;
				}
			}
			$form->populate($data);
			$this->view->form = $form;
			$this -> view -> data = $data;
		}
		public function ajaxDeleteAction(){
		
			$id = $this->_request->getPost('data');
			if($id){
				if($this->_model->deleteItem($id) == TRUE )
				{
					$tmp = 1;
				}
				else{
					$tmp = 2;
				}
			}
			
			$result = array('result'=>$tmp);
			echo json_encode($result);exit;
		}
		
	}
?>